package com.qf.test;

import com.qf.fangdongimpl.FangDongImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 *
 * cglib动态代理：没有接口
 */
@SuppressWarnings("all")
public class CgLibProxy {

    public static void main(String[] args) {
        //创建真实对象
        FangDongImpl fangDong= new FangDongImpl();

        //创建增强对象
        Enhancer enhancer = new Enhancer();

        //设置代理对象的父类
        enhancer.setSuperclass(FangDongImpl.class);

        //辅助部分
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                System.out.println("代理：带客户看房子");
                System.out.println("代理：给客户介绍房子");
                Object invoke = method.invoke(fangDong, args);
                System.out.println("代理：收租");
                return invoke;
            }
        });
        FangDongImpl o = (FangDongImpl) enhancer.create();
        o.ZuFang();
    }

}
